package com.luojilab.componentdemo.application;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.luojilab.component.componentlib.log.ILogger;
import com.luojilab.component.componentlib.router.Router;
import com.luojilab.component.componentlib.router.ui.UIRouter;
import com.luojilab.componentdemo.BuildConfig;
import com.samge.tools.tinkercomponent.util.TinkerUtil;
import com.tencent.bugly.beta.Beta;
import com.tencent.mm.opensdk.utils.Log;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

import org.github.jimu.msg.EventManager;

/**
 * Created by mrzhang on 2017/6/15.
 * <p>
 * edit by samge on 2018/07/06
 */
public class ApplicationLike  extends DefaultApplicationLike {
    private static Application mContext;
    public ApplicationLike(Application application, int tinkerFlags,
                                     boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
                                     long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    {
        Log.d("BaseApplication", "BuildConfig.WEIXIN_ID=" + BuildConfig.WEIXIN_ID +
                "\nBuildConfig.WEIXIN_SID=" + BuildConfig.WEIXIN_SID +
                "\nBuildConfig.SINA_ID=" + BuildConfig.SINA_ID +
                "\nBuildConfig.SINA_SID=" + BuildConfig.SINA_SID +
                "\nBuildConfig.QQ_ID=" + BuildConfig.QQ_ID +
                "\nBuildConfig.QQ_SID=" + BuildConfig.QQ_SID
        );
        PlatformConfig.setWeixin(BuildConfig.WEIXIN_ID, BuildConfig.WEIXIN_SID);
        PlatformConfig.setSinaWeibo(BuildConfig.SINA_ID, BuildConfig.SINA_SID, "http://sns.whalecloud.com/sina2/callback");
        PlatformConfig.setQQZone(BuildConfig.QQ_ID, BuildConfig.QQ_SID);
    }

    private void initUM() {
        UMConfigure.init(mContext, UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        UMConfigure.setEncryptEnabled(!BuildConfig.DEBUG);

        UMShareConfig configUM=new UMShareConfig();
        configUM.isNeedAuthOnGetUserInfo(true);//每次授权
        configUM.isOpenShareEditActivity(true);//是否选择分享编辑页
        configUM.setSinaAuthType(UMShareConfig.AUTH_TYPE_SSO);
        UMShareAPI.get(getApplication()).setShareConfig(configUM);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplication();

        TinkerUtil.initBugly(getApplication(), "32a518ed9f", BuildConfig.DEBUG);
        onTinkerApplicationCreate();
        initUM();
    }

    /**
     * 子类使用此方法
     */
    protected void onTinkerApplicationCreate() {
        ILogger.logger.setDefaultTag("JIMU");
        UIRouter.enableDebug();
        //        EventManager.appendMapper("com.luojilab.componentdemo.application", MainProcessMsgService.class);
        //        EventManager.appendMapper("com.luojilab.componentdemo.application:remote", RemoteMsgService.class);

        EventManager.init(getContext());

        UIRouter.getInstance().registerUI("app");

        //如果isRegisterCompoAuto为false，则需要通过反射加载组件
        Router.registerComponent("com.luojilab.reader.applike.ReaderAppLike");
        //        Router.registerComponent("com.luojilab.share.applike.ShareApplike");
    }

    /**
     * 获取上下文
     * @return
     */
    public static Application getContext(){
        return mContext;
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(base);
        Beta.installTinker(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }
}
